package com.guoyansoft.model.service.impl;

import com.guoyansoft.config.PasswordEncryption;
import com.guoyansoft.config.R;
import com.guoyansoft.model.dao.AddressDao;
import com.guoyansoft.model.dao.CustomerAndAddressDao;
import com.guoyansoft.model.dao.CustomerDao;
import com.guoyansoft.model.pojo.Address;
import com.guoyansoft.model.pojo.Customer;
import com.guoyansoft.model.pojo.CustomerAndAddress;
import com.guoyansoft.model.pojo.CustomerStar;
import com.guoyansoft.model.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
@Service
public class CustomerServiceImpl implements CustomerService {
    @Autowired
    private CustomerDao customerDao;
    @Autowired
    private AddressDao addressDao;
    @Autowired
    private CustomerAndAddressDao customerAndAddressDao;
    @Override
    public R queryByPage(Map<String, Object> params) {
        Long total = customerDao.selectTotal(params);
        List<Customer> customerList = customerDao.selectByPage(params);
        return R.suc(total,customerList);
    }

    @Override
    public R save(Map<String,Object> params) {
        Customer customer = (Customer) params.get("customer");
        Integer customerStarId = (Integer) params.get("customerStarId");
        Address address = (Address) params.get("address");
        customer.setCustomerStar(new CustomerStar(customerStarId,null));
        customer.setPass(PasswordEncryption.getCiphertext(customer.getPass()));
        Long row = addressDao.insert(address);
        Address newAddress = addressDao.selectByAddress(address);
        Long row1 = customerDao.insert(customer);
        Customer newCustomer = customerDao.selectByCustomer(customer);
        CustomerAndAddress customerAndAddress=new CustomerAndAddress();
        customerAndAddress.setCustomer(newCustomer);
        customerAndAddress.setAddress(newAddress);
        int row2 = customerAndAddressDao.insert(customerAndAddress);
        return row1>0&&row2>0&&row>0?R.suc():R.fail();
    }

    @Override
    public R drop(Integer id) {
        CustomerAndAddress customerAndAddress=new CustomerAndAddress();
        Customer customer=new Customer();
        customer.setId(id);
        customerAndAddress.setCustomer(customer);
        customerAndAddressDao.delete(customerAndAddress);
        List<CustomerAndAddress> customerAndAddresses = customerAndAddressDao.selectByCustomerIds(customerAndAddress);
        Long row1=null;
        for (int i = 0; i < customerAndAddresses.size(); i++) {
            Integer[] customerIds=new Integer[customerAndAddresses.size()];
            customerIds[i]=customerAndAddresses.get(i).getAddress().getId();
            row1 = addressDao.delete(customerIds);
        }
        Long row = customerDao.delete(id);
        return row>0?R.suc():R.fail();
    }

    @Override
    public R modify(Map<String,Object> params) {
        Customer customer = (Customer) params.get("customer");
        customer.setPass(PasswordEncryption.getCiphertext(customer.getPass()));
        Integer customerStarId = (Integer) params.get("customerStarId");
        customer.setCustomerStar(new CustomerStar(customerStarId,null));
        Long row = customerDao.update(customer);
        return row>0?R.suc():R.fail();
    }

    @Override
    public R queryByPhone(Map<String,String> params) {
        Customer customer = customerDao.selectByPhone(params);
        return customer==null?R.fail():R.suc(customer);
    }

    @Override
    public Customer queryByCustomer(Customer customer) {
        return customerDao.selectByCustomer(customer);
    }
}
